"""
@author: sumeng
@file: 猜拳游戏.py
@time: 2023/4/14 17:15
"""
#用户可以创建房间，加入房间
#加入房间之后，房间里面要等于两个人，这个房间里面的人就可以开始游戏了
#两个人互相猜拳，按任意键A、B两个人随机出剪刀石头布，输的人扣100金豆
#直到金豆输完了，或按q退出

#房间类
#   属性：房间id、房间里面的人--[]
#   方法：进入房间、退出房间

#用户类
#   属性：用户id、金豆、属于哪个房间
#   方法：猜拳、创建房间、查看房间人数
import random
import sys

class Room:
    def __init__(self, room_id):
        self.room_id = room_id
        self.people = []

class User:
    def __init__(self, user_id, gold_bean):
        self.user_id = user_id
        self.gold_bean = gold_bean
        self.belong_room = None

    #创建房间
    def build_room(self, room_id):
        #实例化一个房间
        room = Room(room_id)
        print(f"创建{room.room_id}房间成功！")
        return room

    #进入房间
    def in_room(self, room_id, room):
        if self.belong_room == None and len(room.people) < 2:
            self.belong_room = room_id
            room.people.append(self.user_id)
            print(f"你已进入{self.belong_room}房间！")
        elif self.belong_room != None:
            print(f"你已在{self.belong_room}房间中，无法再进入房间！")
        else:
            print(f"房间已有{room.people}用户，人数已满，请重新选择房间进入！")

    def out_room(self, room):
        print(f"成功退出{self.belong_room}房间！")
        self.belong_room = None
        room.people.remove(self.user_id)

    def check_room(self, room):
        if len(room.people) == 0:
            print("房间为空！")
        else:
            print(f"房间人数为{len(room.people)}人，房间用户为{room.people}")

    def begin_game(self, another_user):
        game_dict = {0: "石头", 1: "剪刀", 2: "布"}
        g = random.randint(0, 2)
        g_another = random.randint(0, 2)
        print(f"{self.user_id}出{game_dict[g]}，{another_user.user_id}出{game_dict[g_another]}")
        if g == g_another:
            print("平局！")
        elif g == 0 and g_another == 1 or g == 1 and g_another == 2 or g == 2 and g_another == 0:
            another_user.gold_bean -= 100
            print(f"{self.user_id}用户胜利")
            print(f"{another_user.user_id}用户失败，扣除100金豆，剩余金豆数为{another_user.gold_bean}")
        else:
            self.gold_bean -= 100
            print(f"{another_user.user_id}用户胜利")
            print(f"{self.user_id}用户失败，扣除100金豆，剩余金豆数为{self.gold_bean}")


print("欢迎进入猜拳游戏！".center(40, "*"))
room_dict = {}
user_dict = {}
while 1:
    user_id = input("请输入你的用户id(按q退出游戏):")
    if user_id == "q":
        break
    # 保证同一个用户可以多次进入游戏
    if user_id in user_dict:
        user = user_dict[user_id]
        print(f"欢迎{user_id}用户再次进入游戏！你的金豆数为{user_dict[user_id].gold_bean}")
    else:
        print(f"欢迎{user_id}用户！", "提示：用户初始金豆数为500！")
        user = User(user_id, 500)
        user_dict[user_id] = user
    while 1:
        c1 = input("1、创建房间\n2、进入房间\n3、退出房间\n4、查看房间人数\n5、猜拳\n请输入你的选择(按q退出用户):")
        if c1 == "q":
            break
        elif c1 == "1":
            room_id = input("请输入你要创建的房间id：")
            if room_id in room_dict:
                print("房间已存在，请重新输入！")
                continue
            room_dict[room_id] = user.build_room(room_id)
        elif c1 == "2":
            print(f"已有房间号为{room_dict}")
            option = input("输入你想要进入的房间序号：")
            user.in_room(option, room_dict[option])
        elif c1 == "3":
            if user.belong_room == None:
                print("你不在房间中，无法退出！")
            else:
                # 记录用户所在的房间id
                room = user.belong_room
                # 传入用户所在的房间实例
                user.out_room(room_dict[room])
        elif c1 == "4":
            print(f"已有房间为{room_dict}")
            check_id = input("输入你想查询的房间id：")
            if check_id not in room_dict:
                print("不存在该房间！")
            else:
                user.check_room(room_dict[check_id])
        elif c1 == "5":
            belong_room = user.belong_room
            room = room_dict[belong_room]
            lst = room.people
            if len(lst) < 2:
                print("人数不足，无法开始游戏！")
                continue
            #取出另一个用户
            if lst.index(user_id) == 0:
                another_user_id = lst[1]
            else:
                another_user_id = lst[0]
            print(f"另一个用户为{another_user_id}")
            another_user = user_dict[another_user_id]
            user.begin_game(another_user)
            if another_user.gold_bean == 0 or user.gold_bean == 0:
                print("房间内存在用户金豆数不足，游戏结束！")
                sys.exit()
        else:
            print("输入错误，请重新输入！")